package com.fushenlan.epidemicControl.config;

import lombok.extern.slf4j.Slf4j;
import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;
import org.jasypt.encryption.pbe.config.EnvironmentPBEConfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.PropertiesPropertySource;

import java.util.Properties;

/**
 * @author Administrator
 * @date 2022/5/9 0:48
 */
@Slf4j
public class SafetyEncryptProcessor implements EnvironmentPostProcessor {

    public static final String SPRING_DATASOURCE_PASSWORD = "spring.datasource.password";
    public static final String SPRING_DATASOURCE_USERNAME = "spring.datasource.username";

    //这是加解密的盐
    public static final String JASYPT_ENCRYPTOR_PASSWORD = "jasypt.encryptor.password";

    private static final String ALGORITHM = "PBEWithMD5AndDES";

    @Override
    public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
        String jasyptPwd = environment.getProperty(JASYPT_ENCRYPTOR_PASSWORD);
        //加密后的密码
        String password = environment.getProperty(SPRING_DATASOURCE_PASSWORD);
        String username = environment.getProperty(SPRING_DATASOURCE_USERNAME);
        Properties properties = new Properties();
        String decryptPwd = "";
        String decryptUserName = "";
        try {
            assert password != null;
            decryptPwd = decrypt(password, jasyptPwd);
            assert username != null;
            decryptUserName = decrypt(username, jasyptPwd);
            log.error("datasource 解密成功");
        } catch (Exception e) {
            log.error("datasource 解密失败", e);
        }

        properties.setProperty(SPRING_DATASOURCE_PASSWORD, decryptPwd);
        properties.setProperty(SPRING_DATASOURCE_USERNAME, decryptUserName);
        PropertiesPropertySource propertiesPropertySource = new PropertiesPropertySource(SPRING_DATASOURCE_PASSWORD, properties);
        environment.getPropertySources().addFirst(propertiesPropertySource);

    }

    /**
     * 解密
     *
     * @param encryptedText 密文 ENC(eTKWjnYaNGsHJ6kI4RCDJ2QQuKAlbxQX)
     */
    public String decrypt(String encryptedText, String pwd) {
        StandardPBEStringEncryptor standardPBEStringEncryptor = new StandardPBEStringEncryptor();
        EnvironmentPBEConfig config = new EnvironmentPBEConfig();
        // 加密的算法，这个算法是默认的
        config.setAlgorithm(ALGORITHM);
        //加密的密钥
        config.setPassword(pwd);
        standardPBEStringEncryptor.setConfig(config);
        //如果是ENC(开头，以)结尾，去掉ENC()
        if (encryptedText.startsWith("ENC(") && encryptedText.endsWith(")")) {
            encryptedText = encryptedText.substring(3, encryptedText.length() - 1);
        }
        String decryptedText = standardPBEStringEncryptor.decrypt(encryptedText);
        log.info("Decrypted text:{}", decryptedText);
        return decryptedText;
    }
}
